package vitaliy.bondar.diamondcave;

import java.util.ArrayList;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.opengl.util.GLState;

import vitaliy.bondar.diamondcave.SceneManager.SceneType;
import android.content.SharedPreferences.Editor;

public class OptionsScene extends BaseScene
{
    @Override
    public void createScene()
    {
    	createBackground();
    	createOptions();
    }

    @Override
    public void onBackKeyPressed()
    {
        SceneManager.getInstance().loadMenuScene(engine);
    }

    @Override
    public SceneType getSceneType()
    {
        return SceneType.SCENE_OPTIONS;
    }

    @Override
    public void disposeScene()
    {
        activity.runOnUpdateThread(new Runnable()
        {
            @Override
            public void run()
            {
                setChildrenIgnoreUpdate(true);
                setIgnoreUpdate(true);
                detachChildren();
                ResourcesManager.getInstance().unloadOptionsResources();
            }
        });
    }
    
    private void createBackground()
    {
        attachChild( new Sprite( 0, 0, resourcesManager.options_background_region, vbom )
        {
            @Override
            protected void preDraw(GLState pGLState, Camera pCamera) 
            {
                super.preDraw(pGLState, pCamera);
                pGLState.enableDither();
            }
        });
    }
    
    private void createOptions()
    {
        final Text musicLabel = new Text(120, 180, resourcesManager.menu_font, activity.getString(R.string.music), vbom);
        final Text soundLabel = new Text(120, 280, resourcesManager.menu_font, activity.getString(R.string.sounds), vbom);
        final Text languageLabel = new Text(120, 380, resourcesManager.menu_font, activity.getString(R.string.language), vbom);
        
        attachChild(musicLabel);
        attachChild(soundLabel);
        attachChild(languageLabel);
        
        final CheckBox musicCheckBox = new CheckBox(600, 180, vbom, new OnToggledListener() {
			
			@Override
			public void onToggled(boolean state) {
		        Editor ed = prefrences.edit();
		        ed.putBoolean("musicOn", state);
		        ed.commit();
			}
		});
        
        final CheckBox soundCheckBox = new CheckBox(600, 280, vbom, new OnToggledListener() {
        	@Override
        	public void onToggled(boolean state) {
		        Editor ed = prefrences.edit();
		        ed.putBoolean("soundOn", state);
		        ed.commit();
        	}
        });
        
        final TextSelector langSelector = new TextSelector(500, 380, resourcesManager.menu_font, vbom, 
                new OnCurrentChangedListener()
        {
            @Override
            public void onCurrentChanged(String value)
            {
                Editor ed = prefrences.edit();
                ed.putString("language", value);
                ed.commit();
                
                activity.setLocale(value);
            }
        });
        
		
		ArrayList < String > list = new ArrayList < String >(2);
		list.add("eng");
		list.add("rus");
		langSelector.blockSignals(true);
		langSelector.setList(list);
        
        langSelector.setCurrentText(prefrences.getString("language", "eng"));
        langSelector.blockSignals(false);
        
        float size = musicLabel.getHeight();
        musicCheckBox.setSize(size, size);
        soundCheckBox.setSize(size, size);
        langSelector.setSize(260, size);
        
        attachChild(musicCheckBox);
        attachChild(soundCheckBox);
        attachChild(langSelector);
        
        this.registerTouchArea(musicCheckBox);
        this.registerTouchArea(soundCheckBox);
        this.registerTouchArea(langSelector);
        
        musicCheckBox.setChecked(prefrences.getBoolean("musicOn", true));
        soundCheckBox.setChecked(prefrences.getBoolean("soundOn", true));
    }
}